﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StereoMpcSubEngine.cs" company="ScRePt">
//   Copyright © ScRePt 2010 - 2012
// </copyright>
// <summary>
//   The stereo mpc sub engine.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MP3D.Subtitles
{
    #region Using Directives

    using System.Drawing;

    using MediaPortal.Player.Subtitles;

    using Stereoscopic;

    #endregion

    /// <summary>
    /// The stereo mpc sub engine.
    /// </summary>
    public class StereoMpcSubEngine : StereoSubEngine
    {
        #region Constants and Fields

        /// <summary>
        /// The i.
        /// </summary>
        private int i;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="StereoMpcSubEngine"/> class.
        /// </summary>
        /// <param name="baseEngine">
        /// The base engine.
        /// </param>
        public StereoMpcSubEngine(ISubEngine baseEngine)
            : base(baseEngine)
        {
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The render.
        /// </summary>
        /// <param name="subsRect">
        /// The subs rect.
        /// </param>
        /// <param name="frameRect">
        /// The frame rect.
        /// </param>
        public override void Render(Rectangle subsRect, Rectangle frameRect)
        {
            int parallaxFactorX = (int)((this.HorizontalParallax / 2d) * ((double)subsRect.Width / (this.VideoWidth == 0 ? 1 : this.VideoWidth)));
            int parallaxFactorY = (int)((this.VerticalParallax / 2d) * ((double)subsRect.Height / (this.VideoHeight == 0 ? 1 : this.VideoHeight)));

            if (this.i++ <= 1)
            {
                return; // skip first 2 frames ... unknown mpc crash
            }

            switch (this.OutputFormat)
            {
                case CONSTS.METHOD_OVERUNDER1:
                case CONSTS.METHOD_OVERUNDER2:

                    // Over side
                    subsRect.Y = parallaxFactorY;
                    subsRect.Height = subsRect.Height / 2;
                    base.Render(subsRect, frameRect);

                    // Under side
                    subsRect.Y = subsRect.Height - parallaxFactorY;
                    base.Render(subsRect, frameRect);
                    break;
                case CONSTS.METHOD_SIDEBYSIDE1:
                case CONSTS.METHOD_SIDEBYSIDE2:

                    // Left side
                    subsRect.X += parallaxFactorX;
                    frameRect.X += parallaxFactorX;
                    subsRect.Y -= parallaxFactorY;
                    frameRect.Y -= parallaxFactorY;
                    subsRect.Width = subsRect.Width / 2;
                    frameRect.Width = frameRect.Width / 2;
                    base.Render(subsRect, frameRect);

                    // Right side
                    subsRect.X = subsRect.Width - parallaxFactorX;
                    frameRect.X += frameRect.Width - (parallaxFactorX * 2);
                    subsRect.Y += parallaxFactorY;
                    frameRect.Y += parallaxFactorY;
                    base.Render(subsRect, frameRect);
                    break;
                default:
                    base.Render(subsRect, frameRect);
                    break;
            }
        }

        #endregion
    }
}